package com.example.object;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Roads {

	public String Road_ID;

	public String Road_Name;

	public String District_ID;

	public String District_Name;

	public String IsActive;

	public String ModifiedDate;

	public String Action;

	public String getRoad_ID() {
		return Road_ID;
	}

	public void setRoad_ID(String road_ID) {
		Road_ID = road_ID;
	}

	public String getRoad_Name() {
		return Road_Name;
	}

	public void setRoad_Name(String road_Name) {
		Road_Name = road_Name;
	}

	public String getDistrict_ID() {
		return District_ID;
	}

	public void setDistrict_ID(String district_ID) {
		District_ID = district_ID;
	}

	public String getDistrict_Name() {
		return District_Name;
	}

	public void setDistrict_Name(String district_Name) {
		District_Name = district_Name;
	}

	public String getIsActive() {
		return IsActive;
	}

	public void setIsActive(String isActive) {
		IsActive = isActive;
	}

	public String getModifiedDate() {
		return ModifiedDate;
	}

	public void setModifiedDate(String modifiedDate) {
		ModifiedDate = modifiedDate;
	}

	public String getAction() {
		return Action;
	}

	public void setAction(String action) {
		Action = action;
	}

	public static ArrayList<District> getListRoad(String text)
			throws JSONException {
		ArrayList<District> lstDistrict = new ArrayList<District>();
		int countDistrict = 0;
		JSONArray jArr = new JSONArray(text);
		for (int i = 0; i < jArr.length(); i++) {
			JSONObject jObj = jArr.getJSONObject(i);
			String idDistrict = jObj.getString("District_ID");
			Roads road = new Roads();
			road.setAction(jObj.getString("Action"));
			road.setDistrict_ID(jObj.getString("District_ID"));
			road.setDistrict_Name(jObj.getString("District_Name"));
			road.setIsActive(jObj.getString("IsActive"));
			road.setModifiedDate(jObj.getString("ModifiedDate"));
			road.setRoad_ID(jObj.getString("Road_ID"));
			road.setRoad_Name(jObj.getString("Road_Name"));
			if (countDistrict == 0
			|| 
			(!idDistrict.equals(lstDistrict.get(countDistrict - 1).getDistrict_ID()) && countDistrict >= 1))
			{
				District district = new District();
				district.setDistrict_ID(jObj.getString("District_ID"));
				district.setDistrict_Name(jObj.getString("District_Name"));
				district.getLstRoads().add(road);
				lstDistrict.add(district);
				countDistrict++;
			} else {

				lstDistrict.get(countDistrict-1).getLstRoads().add(road);

			}
			// add new roads

		}
		return lstDistrict;

	}
}
